package com.party.admin.biz.copartner;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.party.admin.web.dto.input.common.CommonQuery;
import com.party.common.paging.Page;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.core.model.YesNoStatus;
import com.party.core.model.copartner.TargetCopartner;
import com.party.core.model.distributor.DistributorRelationType;
import com.party.core.model.member.CopartnerAnalyze;
import com.party.core.model.member.CopartnerAnalyzeQuery;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberCopartnerCount;
import com.party.core.model.wallet.Withdrawals;
import com.party.core.service.copartner.IBrokerageItemService;
import com.party.core.service.copartner.ITargetCopartnerService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.distributor.IDistributorCountService;
import com.party.core.service.member.ICopartnerAnalyzeService;
import com.party.core.service.member.IMemberActService;
import com.party.core.service.member.IMemberCopartnerCountService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.order.IOrderFormService;
import com.party.core.service.wallet.biz.WithdrawalsBizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.party.common.utils.DateUtils.addMonth;

/**
 * 合伙人数据分析
 * Created by wei.li
 *
 * @date 2017/7/19 0019
 * @time 14:33
 */
@Service
public class CopartnerAnalyzeBizService {

    @Autowired
    private ICopartnerAnalyzeService copartnerAnalyzeService;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private IMemberActService memberActService;

    @Autowired
    private IOrderFormService orderFormService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IBrokerageItemService brokerageItemService;

    @Autowired
    private WithdrawalsBizService withdrawalsBizService;

    @Autowired
    private ITargetCopartnerService targetCopartnerService;

    @Autowired
    private IDistributorCountService distributorCountService;

    @Autowired
    private IMemberCopartnerCountService memberCopartnerCountService;

    /**
     * 合伙人数据分析
     *
     * @param analyzeQuery 查询参数
     * @param page         分页参数
     * @return 分析列表
     */
    public List<CopartnerAnalyze> list(CopartnerAnalyzeQuery analyzeQuery, Page page) {
        if (null != analyzeQuery.getTimeType() && !new Integer(0).equals(analyzeQuery.getTimeType())) {
            CommonQuery commonQuery = new CommonQuery();
            commonQuery.setTimeType(analyzeQuery.getTimeType());
            analyzeQuery.setCreateStart(commonQuery.getCreateStart());
            analyzeQuery.setCreateEnd(commonQuery.getCreateEnd());
        }
        return copartnerAnalyzeService.sumList(analyzeQuery, page);
    }

    /**
     * 今天的数据
     */
    public void todayData() {
        Member member = new Member();
        member.setIsCopartner(YesNoStatus.YES.getCode());
        List<Member> list = memberService.list(member);
        String today = DateUtils.todayDate();
        for (Member m : list) {

            // 报名数
            Integer applyNum = memberActService.count(m.getId(), today, today, true);

            // 订单数
            Integer orderNum = orderFormService.count(today, today, m.getId(), true);

            // 众筹成功人数
            Integer successNum = projectService.countByDate(m.getId(), today, today);

            // 累计资金
            Double totalMoney = orderFormService.paymentSum(today, today, m.getId(), true);

            // 累计佣金
            Double totalBrokerage = brokerageItemService.brokerageSum(m.getId(), today, today, null);
            CopartnerAnalyze copartnerAnalyze = copartnerAnalyzeService.getSet(m.getId(), DateUtils.today());
            copartnerAnalyze.setApplyNum(applyNum);
            copartnerAnalyze.setOrderNum(orderNum);
            copartnerAnalyze.setSuccessNum(successNum);
            copartnerAnalyze.setTotalMoney(totalMoney);
            copartnerAnalyze.setTotalBrokerage(totalBrokerage);
            copartnerAnalyze.setRegisterDate(m.getCreateDate());
            copartnerAnalyze.setLogo(m.getLogo());
            copartnerAnalyze.setName(m.getRealname());
            copartnerAnalyze.setMobile(m.getMobile());
            copartnerAnalyzeService.update(copartnerAnalyze);
        }
    }

    /**
     * 合伙人项目数据统计
     */
    public void copartnerActivityData() {
        Member member = new Member();
        member.setIsCopartner(YesNoStatus.YES.getCode());
        List<Member> list = memberService.list(member);
        for (Member m : list) {
            List<TargetCopartner> targetCopartners = targetCopartnerService.findByPartnerId(m.getId());
            for (TargetCopartner targetCopartner : targetCopartners) {
                Map<String, Object> resultMap = orderFormService.getZcActivityOrder(targetCopartner.getTargetId());
                Integer orderNum = ((Long) resultMap.get("orderNum")).intValue();
                Double totalMoney = (Double) resultMap.get("totalMoney");
                if (totalMoney == null) {
                    totalMoney = 0d;
                }
                TargetCopartner dbTarget = targetCopartnerService.get(targetCopartner.getId());

                // 订单数
                dbTarget.setOrderNum(orderNum);

                // 累计金额
                dbTarget.setTotalMoney(totalMoney);

                // 累计佣金
                Double totalBrokerage
                        = brokerageItemService.brokerageSum(null, null, null, targetCopartner.getTargetId());
                dbTarget.setTotalBrokerage(totalBrokerage);
                targetCopartnerService.update(dbTarget);
            }
        }
    }

    /**
     * 获取佣金数据
     * @return
     */
    public Map<String, Object> getBrokerageData() {
        Map<String, Object> resultMap = Maps.newHashMap();
        MemberCopartnerCount copartnerCount = memberCopartnerCountService.sum(new MemberCopartnerCount());

        // 累计提现
        Double withdrawalTotal = withdrawalsBizService.getWithdrawalTotal(null, Withdrawals.TYPE_BROKERAGE,null);

        resultMap.put("withdrawalTotal", BigDecimalUtils.round(withdrawalTotal, 2));
        resultMap.put("canWithdrawalTotal", copartnerCount.getCanWithdrawal());
        resultMap.put("remainBrokerageTotal", copartnerCount.getNotBalance());
        resultMap.put("totalMoney", copartnerCount.getTotalMoney());
        resultMap.put("brokerageTotal", copartnerCount.getTotalBrokerage());

        return resultMap;
    }

    /**
     * 佣金数字统计
     *
     * @return 佣金集合
     */
    public Map<String, Double> getSumTotal() {

        // 累计佣金
        Double brokerageTotal = brokerageItemService.getSumTotal(null, 1);

        // 累计提现
        Double withdrawalTotal = withdrawalsBizService.getWithdrawalTotal(null, Withdrawals.TYPE_BROKERAGE,null);

        Map<String, Double> resultMap = Maps.newHashMap();

        // 累计佣金
        resultMap.put("brokerageTotal", brokerageTotal);

        // 已提现金额
        resultMap.put("withdrawalTotal", withdrawalTotal);

        // 未结算佣金
        double subTotal = BigDecimalUtils.sub(brokerageTotal, withdrawalTotal);
        subTotal = BigDecimalUtils.round(subTotal, 2);
        resultMap.put("remainBrokerageTotal", subTotal);
        return resultMap;
    }
}
